package info.cs52.fes.ajax;

import info.cs52.fes.lp.LanguageParser;
import info.cs52.fes.lp.NatureLanguageParser;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;



public class ChatHandle extends AjaxHandle {

	private final String[] RAN_MSG = {
			"Chào bạn, bạn khỏe chứ?",
			"Xin chào, Tôi có thể làm gì giúp bạn?",
			"Thời tiết hôm nay đẹp nhỉ, Hum nay cùng ăn một bữa thật ngon miệng nhé."
			};
	
	

	@Override	               
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		//get an Enumeration of all the request header names
	      
	    
	      resp.setContentType("JSON");	
	      resp.setCharacterEncoding("UTF-8");
	      java.io.PrintWriter out = resp.getWriter();
	      
	      //can xu ly message cua nguoi dung, neu co dau xuong dong la loi
	      out.println("{\"user\": \""+getUserName() + ": " + getUserMessage(req) +"\", ");
	      out.println("\"fesser\": \" Fesser: " + getFesserMsg(req)+"\", ");
	      out.println("\"recommender\": {\"meal01\":  {\"dish01\":\"thit_lon_kho_nuoc_dua\", \"dish02\":\"thit_bo_bit_tet\"}}}");

	}
	
	protected String getFesserMsg(HttpServletRequest req) throws IOException{
		//get from PL
		
		//test NLP
		String result = req.getParameter("user-msg").replace('\n', ' ').replaceAll(" +", " ").trim();
		//preprocess
		NatureLanguageParser nlp = new NatureLanguageParser();
		nlp.setOriginalPhrase(StringEscapeUtils.escapeJavaScript(result));
		nlp.setDefaultPattern(LanguageParser.ANSWER_PATTERNS);
		nlp.parse();
		
		return nlp.getTranslatedPhrase();
	}
	
	protected String getUserName(){
		//get later
		return "You";
	}
	protected String getUserMessage(HttpServletRequest req){
		String result = req.getParameter("user-msg").replace('\n', ' ').replaceAll(" +", " ").trim();
		//preprocess
		return StringEscapeUtils.escapeJavaScript(result);
	}
}
